package com.lanou.sfu.songforu.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.SearchResultAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by quyan on 15/10/28.
 */
public class SearchResultFragment extends BaseFragment  {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private SearchResultAdapter adapter;
    private String url;
    private List<Fragment> data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_searchdetail,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tabLayout = (TabLayout) getActivity().findViewById(R.id.mtablayout);
        viewPager = (ViewPager) getActivity().findViewById(R.id.mviewpager);
        adapter = new SearchResultAdapter(getFragmentManager());
        viewPager.setAdapter(adapter);//给ViewPager设置适配器
        tabLayout.setupWithViewPager(viewPager);//将TabLayout和ViewPager关联起来。
        tabLayout.setBackgroundColor(0xbbbbbb);
        tabLayout.setDrawingCacheBackgroundColor(0xcccccc);
//        songSearchBack.setOnClickListener(this);
        Bundle bundle = getArguments();
        url = bundle.getString("url");
        Log.i("chenga", url + "");
        addFragment();

    }

    //添加Fragment
    public void addFragment(){
        data = new ArrayList<>();
        data.add(SearchingSingleSongFragment.passData(getActivity(), url));
        data.add(SearchingSingerFragment.passData(getActivity(), url));
        data.add(SearchingAlbumFragment.passData(getActivity(), url));
        data.add(SearchingMVFragment.passData(getActivity(), url));
        adapter.setData(data);
    }

//    //返回
//    @Override
//    public void onClick(View v) {
//        getFragmentManager().popBackStack();
//    }

    //移除Fragment
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        for (Fragment fragment : data){
            getFragmentManager().beginTransaction().remove(fragment).commit();
        }
    }
}
